package com.atguigui.leetcode;

import java.util.HashMap;

/**
 * 1742.盒子中小球的最大数量
 * Project: leetcode
 * Package: com.atguigui.leetcode
 * Version: 1.0
 * <p>
 * Created by WJX on 2022/11/23 11:15
 */
public class P1742MaximumNumberOfBallsInABox {
    public static void main(String[] args) {
        Solution solution = new P1742MaximumNumberOfBallsInABox().new Solution();
        // TO TEST
    }

    class Solution {
        public int countBalls(int lowLimit, int highLimit) {

            HashMap<Integer, Integer> ans = new HashMap();
            int res = 0;
            for (int i = lowLimit; i < highLimit + 1; i++) {
                String num = (i + "");
                if (num.length() > 1) {
                    int key = 0;
                    for (int j = 0; j < num.length(); j++) {
                        String chr = num.substring(j, j + 1);
                        key += Integer.parseInt(chr);
                    }
                    ans.put(key, ans.getOrDefault(key, 0) + 1);
                    res = Math.max(res, ans.get(key));
                } else {
                    ans.put(i, ans.getOrDefault(i, 0) + 1);
                    res = Math.max(res, ans.get(i));
                }
            }

            return res;
        }
    }
}
